﻿module Sustainalytics.Notifications.Admin {
    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;

    export class NotificationsService {

        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjectsAdmin: CommonAdmin.IStaticObjects;
        private $location: ng.ILocationService;
        private $upload: any;
        private internalTable = {};

        constructor(
            $q: ng.IQService,
            $http: ng.IHttpService,
            $location: ng.ILocationService,
            notificationFactory: Common.INotificationFactory,
            staticObjectsAdmin: CommonAdmin.IStaticObjects,
            $upload: any) {

            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.staticObjectsAdmin = staticObjectsAdmin;
            this.$location = $location;
            this.$upload = $upload;
        }

        private notificationsUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/newsserv/api/v1/notifications";
        }

        //private helperUrl(): string {
        //    var ctx = this;
        //    return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/notificationsserv/api/v1/notificationsnames/";
        //}
        //private filesUrl(): string {
        //    var ctx = this;
        //    return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/notificationsserv/api/v1/files/";
        //}

        public putSetting(key: string, value: any) {
            var ctx = this;
            ctx.internalTable[key] = value;
            console.log(key + "=" + value);
        }

        public getSetting(key: string): any {
            var ctx = this;
            if (ctx.internalTable[key])
                return ctx.internalTable[key];
            return null;
        }


        public getNotification(): ng.IPromise<NotificationDto> {

            var ctx = this;
            var def = this.$q.defer();
            var promise = this.$http.get(ctx.notificationsUrl());
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        //public updateNotification(dto: NotificationDto): ng.IPromise<boolean> {

        //    var ctx = this;
        //    var def = ctx.$q.defer();

        //    console.log(dto);
        //    var promise: ng.IHttpPromise<{}>;

        //    promise = ctx.$http.put(ctx.notificationsUrl(), dto);

        //    promise
        //        .success(data => { def.resolve(true); })
        //        .error(err => { ctx.showError(err); def.reject(); });
        //    return def.promise;
        //}

        public updateNotification(dto: NotificationDto): ng.IHttpPromise<NotificationDto> {

            var ctx = this;

            return ctx.$http.post<NotificationDto>(ctx.notificationsUrl(), dto);
        }
        
        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['errorCode'])
                ctx.notificationFactory.error('', ctx.staticObjectsAdmin.getMessageByInternalCode(err['errorCode']));
        }

    }

    // register service with Notifications Angular module
    var mod = angular.module(notificationsAdminModuleName);

    mod.service('NotificationsService',
        ['$q', '$http', '$location', 'notificationFactory', 'staticObjectsAdmin', '$upload', NotificationsService]);
}